package com.fand.configuration.controller;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.common.service.CommonService;
import com.fand.configuration.vo.DictionaryVo;
import com.fand.dto.PaginationDto;
import com.google.common.collect.Maps;

/**
 * 字典
 * 
 * @author haitao.yao
 */
@Controller
public class DictionaryController {
	/**
	 * 公共服务
	 */
	@Qualifier(CommonService.BEAN_ID)
	@Autowired(required = false)
	private CommonService commonService;

	@RequestMapping(value = { "/admin/configuration/dictionary/reload.json" })
	public @ResponseBody
	Map<String, Object> adminConfigurationDictionaryReload() {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.commonService.getConfigurationService().getDictionaryService().reload();
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/configuration/dictionaryTy/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminConfigurationDictionaryTyDelete(final String ids) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			if (StringUtils.isBlank(ids)) {
				new Exception("请传递字典类型标识参数");
			}

			this.commonService.getConfigurationService().getDictionaryService().deleteDictionaryTyByIds(ids);
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	/**
	 * 字典类型主页面
	 * 
	 * @param DictionaryVo
	 *            字典视图对象
	 * @return 模板路径
	 */
	@RequestMapping(value = { "/admin/configuration/dictionaryTy.htm", "/configuration/dictionaryTy/index.htm" }, method = RequestMethod.GET)
	public String adminConfigurationDictionaryTyindex(final DictionaryVo DictionaryVo) {
		return "admin/configuration/dictionaryTy/index";
	}

	/**
	 * 字典类型编辑
	 * 
	 * @param id
	 *            字典类型标识
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/admin/configuration/dictionaryTy/edit.htm" })
	public String adminSiteDictionaryTyEdit(final String id, final Model model) {
		return "admin/configuration/dictionaryTy/edit";
	}

	/**
	 * 字典类型分页查询
	 * 
	 * @param dictionaryVo
	 *            字典视图对象
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/admin/configuration/dictionaryTy/indexPagination.htm" })
	public String adminSiteDictionaryTyIndexPagination(final DictionaryVo dictionaryVo, final Model model) {
		final PaginationDto<Map<String, Object>> dictionaryTyPagination = this.commonService.getConfigurationService()
				.getDictionaryService().paginationDictionaryTyBy1ForMapList(dictionaryVo.getDictionaryTy());

		model.addAttribute("dictionaryTyPagination", dictionaryTyPagination);

		return "admin/configuration/dictionaryTy/indexPagination";
	}

	/**
	 * 字典类型新健
	 * 
	 * @param dictionaryVo
	 *            字典视图对象
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/admin/configuration/dictionaryTy/new.htm" })
	public String adminSiteDictionaryTyNew(final DictionaryVo dictionaryVo, final Model model) {
		return "admin/configuration/dictionaryTy/new";
	}

	/**
	 * 字典类型查询
	 * 
	 * @param id
	 *            字典类型标识
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/admin/configuration/dictionaryTy/view.htm" })
	public String adminSiteDictionaryTyView(final String id, final Model model) {
		return "admin/configuration/dictionaryTy/view";
	}
}
